;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.ss" ("cce" "dracula.plt") "modular" "lang")
;@author Youming Lin
;@date Apr 8, 2012
;@version 1.0

(require "specifications.lisp")

;MMath provides some commonly used functions
(module MMath
  ;finds sum of a list of numbers
  ;@param nums - list of numbers
  ;@return sum - sum of the list of numbers
  (defun sum (nums)
    (if (consp nums)
        (if (rationalp (car nums))
            (+ (car nums) (sum (cdr nums)))
            (sum (cdr nums))
        )
        0))
  
  ;finds average of a list of numbers
  ;@param nums - list of numbers
  ;@param avg - average of the list of numbers
  (defun average (nums)
    (/ (sum nums) (len nums)))
  
  ;helper function for nth-root
  ;@param n - root
  ;@param x - number in radicand
  ;@param low - lower bound of search space
  ;@param high - upper bound of search space
  ;@return ratp - nth root of x
  (defun n-root-helper (n x low high)
    (let* ((mid (/ (+ low high) 2))
           (check (Expt mid n))
           (offset .0001))
          (if (and (<= check (+ x offset)) (>= check (- x offset)))
              mid
              (if (> check x)
                  (n-root-helper n x low mid)
                  (n-root-helper n x mid high)
              )
          )
    )
  )

  ;finds the nth root of x
  ;@param n - root
  ;@param x - number in radicand
  ;@return ratp - nth root of x
  (defun n-root (n x)
    (n-root-helper n x 0 x)
  )
 
  (export IMath))